package com.emm.yixun.basic.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emm.yixun.basic.mapper.RoleMapper;
import com.emm.yixun.basic.mapper.SalesGroupMapper;
import com.emm.yixun.basic.mapper.SalesGroupUserMapper;
import com.emm.yixun.basic.mapper.UserMapper;
import com.emm.yixun.basic.service.PermissionService;
import com.emm.yixun.common.model.Role;
import com.emm.yixun.common.model.SalesGroup;
import com.emm.yixun.common.model.SalesGroupUser;
import com.emm.yixun.common.model.User;
import com.emm.yixun.common.model.dto.SalesGroupUserDto;

@Service("permissionService")
public class PermissionServiceImpl implements PermissionService{

	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private RoleMapper roleMapper;
	
	@Autowired
	private SalesGroupUserMapper salesGroupUserMapper;
	
	@Autowired
	private SalesGroupMapper salesGroupMapper;
	
	public String projectPermissionCondition(Long userId,String prefix) {
		
		/*
		 * 1.查找用户
		 * 2.查找用户所属角色
		 * 3.判断角色对应的项目权限 0无权限 1销售组 2全部 
		 * 4.组装权限条件sql并返回
         */
		//默认返回无权限字符
		String defaultStr="0: and 1=2 ";
		if(null==userId)return defaultStr;
		User user=userMapper.selectById(userId);
		if(null==user)return defaultStr;
		//超级管理员
		if(user.getIsAdmin().equals("1")){
			return "2: and 1=1";
		}
		Long roleId=user.getRoleId();
		if(null==roleId)return defaultStr;
		Role role=roleMapper.selectById(roleId);
		if(null==role)return defaultStr;
		String projectPermission=role.getProjectPermission();
		if(StringUtils.isBlank(projectPermission))return defaultStr;
		if(projectPermission.equals("0")){
			return defaultStr;
		}else if(projectPermission.equals("2")){
			return "2: and 1=1";
		}else if(projectPermission.equals("1")){
			SalesGroupUserDto salesGroupUserDto=new SalesGroupUserDto();
			salesGroupUserDto.setUserId(userId);
			salesGroupUserDto.setStatus("0");
			List<SalesGroupUser> salesGroupUserList=salesGroupUserMapper.selectListByDto(salesGroupUserDto);
			if(null==salesGroupUserList || salesGroupUserList.size()==0)return defaultStr;
			Map<Long,Long> salesGroupIds=new HashMap<Long,Long>();
			for(SalesGroupUser salesGroupUser : salesGroupUserList){
				salesGroupIds.put(salesGroupUser.getSalesGroupId(), salesGroupUser.getSalesGroupId());
			}
			StringBuffer condition=new StringBuffer();
			condition.append(projectPermission);
			condition.append(": and ");
			condition.append(prefix);
			condition.append(".id in (");
			int count=0;
			for(Long salesGroupId : salesGroupIds.keySet()){
				SalesGroup salesGroup=salesGroupMapper.selectById(salesGroupId);
				if(null==salesGroup || salesGroup.getStatus().equals("1"))continue;
				if(count==0){
					condition.append(salesGroup.getProjectId());
				}else{
					condition.append(",");
					condition.append(salesGroup.getProjectId());
				}
				count++;
			}
			condition.append(") ");
			if(count>0)return condition.toString();
		}
		return defaultStr;
	}

	public String customerPermissionCondition(Long userId, Long merchantId,
			String productCode, String prefix) {
		/*
		 * 1.查找用户
		 * 2.查找用户所属角色
		 * 3.判断角色对应的客户权限 0无权限 1个人 2销售组 3全部 
		 * 4.组装权限条件sql并返回
         */
		//默认返回无权限字符
		String defaultStr="0: and 1=2 ";
		if(null==userId)return defaultStr;
		User user=userMapper.selectById(userId);
		if(null==user)return defaultStr;
		//超级管理员
		if(user.getIsAdmin().equals("1")){
			return "2: and 1=1";
		}
		Long roleId=user.getRoleId();
		if(null==roleId)return defaultStr;
		Role role=roleMapper.selectById(roleId);
		if(null==role)return defaultStr;
		String customerPermission=role.getCustomerPermission();
		if(StringUtils.isBlank(customerPermission))return defaultStr;
		if(customerPermission.equals("0")){//无权限
			return defaultStr;
		}else if(customerPermission.equals("3")){//全部
			return "3: and 1=1";
		}else if(customerPermission.equals("1")){//个人
			return "1:"+ userId;
		}else if(customerPermission.equals("2")){//销售组
			Long salesGroupId = salesGroupMapper.selectGroupId(merchantId, productCode, userId);
			if(salesGroupId == null){
				return defaultStr;
			}
			return "2:"+ salesGroupId;
		}
		return defaultStr;
	}
}
